package com.dark.sys.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dark.common.web.controller.BaseController;
import com.dark.common.web.model.DataGrid;
import com.dark.common.web.validate.AjaxResponse;
import com.dark.sys.entity.Role;
import com.dark.sys.service.RoleResourceService;
import com.dark.sys.service.RoleService;
import com.dark.sys.web.dto.ResourceDto;
import com.dark.sys.web.dto.RoleDto;
import com.dark.sys.web.dto.RoleResourceDto;

@Controller
@RequestMapping("/admin/sys/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService ;
	
	@Autowired
	private RoleResourceService roleResourceService ;
	
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(Model model) {
		return viewName("main") ;
	}
	
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Integer id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public AjaxResponse add(RoleDto dto) {
		this.roleService.add(dto) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
	public AjaxResponse delete(@PathVariable String id) {
		this.roleService.delete(id) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public AjaxResponse deleteBatch(@RequestParam String ids) {
		this.roleService.delete(ids) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public AjaxResponse update(RoleDto dto) {
		this.roleService.update(dto) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	public Role get(@PathVariable Integer id, Model model) {
		return this.roleService.get(id) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/datagrid")
	public DataGrid datagrid(RoleDto dto) {
		return this.roleService.datagrid(dto) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/listAll")
	public List<RoleDto> listAll(RoleDto dto) {
		return this.roleService.listAll(dto) ;
	}
	
	
	
	/**
	 * 分配资源UI
	 * @param id 角色ID
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/ui_resource/{roleIds}", method=RequestMethod.GET)
	public String ui_resource(@PathVariable String roleIds, Model model) {
		model.addAttribute("roleIds", roleIds) ;
		if(null != roleIds && roleIds.length() > 0) {
			String[] split = roleIds.split(",") ;
			if(split.length == 1) {
				RoleResourceDto rr = this.roleResourceService.getOneByRoleId(Integer.parseInt(split[0])) ;
				if(null != rr) {
					model.addAttribute("operIds", rr.getResourceIds()) ;
				}
			}
		}
		return viewName("ui_resource") ;
	}
	
	@ResponseBody
	@RequestMapping(value="/resourceTree")
	public List<ResourceDto> resourceTree(ResourceDto dto) {
		return this.roleResourceService.operationResourceTree(dto) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/addRoleResource", method=RequestMethod.POST)
	public AjaxResponse addRoleResource(RoleResourceDto dto) {
		this.roleResourceService.add(dto) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/getRoleResource", method=RequestMethod.POST)
	public RoleResourceDto getRoleResource(RoleResourceDto dto) {
		return this.roleResourceService.getOneByRoleId(dto.getRoleId()) ;
	}
	
}
